import 'package:flutter/material.dart';
import 'package:flutter_teach/custom_provider/expend/my_consumer.dart';

import '../custom_provider/provider_creator.dart';
import 'vm_add_num.dart';

class AddNumPage extends StatelessWidget {
  const AddNumPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("迷你Provider实现"),
      ),
      body: Column(
        children: [
          ProviderCreator(
            viewModel: AddNumViewModel(),
            child: Center(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  //注意这里要用Builder，这样才可以在调用of方法的时候找到AddNumViewModel，因为层级不一样
                  Builder(builder: (context) {
                    debugPrint("Builder builder 初始值");
                    return Text(
                      //不使用MyConsumer的情况可以用of方法获取
                      "初始值：${ProviderCreator.of<AddNumViewModel>(context, dependOn: false)?.count.toString() ?? "空"}",
                    );
                  }),
                  Builder(builder: (context) {
                    debugPrint("Builder builder 会改变的Text");
                    return Text(
                      //不使用MyConsumer的情况可以用of方法获取
                      ProviderCreator.of<AddNumViewModel>(context)?.count.toString() ?? "空",
                    );
                  }),
                  Builder(builder: (context) {
                    debugPrint("Builder builder ElevatedButton 加1");
                    return ElevatedButton(
                      onPressed: () {
                        //不使用MyConsumer的情况可以用of方法获取
                        ProviderCreator.of<AddNumViewModel>(context, dependOn: false)?.addNum(1);
                      },
                      child: const Text("加1"),
                    );
                  }),
                  Builder(builder: (context) {
                    debugPrint("Builder builder ElevatedButton 加2");
                    return ElevatedButton(
                      onPressed: () {
                        ProviderCreator.of<AddNumViewModel>(context, dependOn: false)?.addNum(2);
                      },
                      child: const Text("加2"),
                    );
                  }),
                  Builder(builder: (context) {
                    debugPrint("Builder builder ElevatedButton 加3");
                    return MyConsumer(
                      consumerWidgetBuilder: (BuildContext context, AddNumViewModel? viewModel) {
                        debugPrint("MyConsumer builder ElevatedButton 加3，dependOn=true");
                        return ElevatedButton(
                          onPressed: () {
                            viewModel?.addNum(3);
                          },
                          child: const Text("加3"),
                        );
                      },
                    );
                  }),
                  MyConsumer(
                    dependOn: false,
                    consumerWidgetBuilder: (BuildContext context, AddNumViewModel? viewModel) {
                      debugPrint("MyConsumer builder ElevatedButton 加4");
                      return ElevatedButton(
                        onPressed: () {
                          viewModel?.addNum(4);
                        },
                        child: const Text("加4"),
                      );
                    },
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
